<template>
    <el-dialog @opened="handleOpen" :modelValue="dialogVisible" @update:modelValue="(e)=>{emit('update:dialogVisible',e)}"   title="Shipping address">
        <el-form :model="formParams">
            <el-form-item label="menuIds" :label-width="formLabelWidth">
                <el-tree-select
                    ref="treeRef"
                    node-key="id"
                    :props="{
                        'label':'displayName'
                    }"
                    v-model="formParams.menuIds"
                    :data="routeDatas"
                    :render-after-expand="false"
                    multiple
                    show-checkbox
                />

            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="emit('update:dialogVisible',false)">Cancel</el-button>
                <el-button type="primary" @click="sureClick">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {rolesCreate} from "@/api/roles.js"
import {apiRoleMenusUpdate} from "@/api/roleMenus"
import {findParents} from "@/utils/tools"
import {uniq} from "lodash-es"
const props = defineProps({
  dialogVisible: {
    type:Boolean,
    default:false
  },
  routeDatas:{
    type:Array,
    default:function() {
      return []
    }
  },
  roleId:{
    type:String,
    default:''
  },
  type:{
    type:String,
    default:'add'
  },
  editObject:{
    type:Object,
    default:function() {
      return null
    }
  }
})
const emit = defineEmits(['update:dialogVisible','hanldeRequest']);
const formLabelWidth = '140px'
const treeRef = ref(null);
const formParams = ref({
  name:'',
  menuIds:[]
})
async function sureClick() {
  const res = [];
  formParams.value.menuIds.forEach((id) => {
    const arr = findParents(props.routeDatas,id);
    res.push(...arr,id);
  });
  console.log('res',res);


  await apiRoleMenusUpdate({
    menuIds:uniq(res),// formParams.value.menuIds, //treeRef.value.getCheckedKeys(false), //
    roleId: props.roleId
  })
}
</script>

<style lang="scss" scoped>

</style>